package app.web.admin.controller;

import app.web.admin.domain.ao.MenuCreateAo;
import app.web.admin.domain.ao.MenuPatchByIdAo;
import app.web.admin.domain.pojo.AdminMenu;
import app.web.admin.service.MenuService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.gitee.feizns.quickstart.domain.page.PageRecordsVo;
import com.gitee.feizns.quickstart.domain.vo.Ret;
import com.gitee.feizns.quickstart.web.domain.query.QueryParamsAo;
import com.gitee.feizns.quickstart.web.restful.Rests;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理模块/菜单
 * @author feizns
 * @since 2025-04-09
 */
@Validated
@SaCheckLogin
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/admin/menu")
public class MenuController {

    /**
     * 菜单服务
     */
    private final MenuService service;

    /**
     * 分页
     * @param query 查询参数
     * @return {@link Ret}<{@link PageRecordsVo}<{@link AdminMenu}>>
     */
    @GetMapping
    public Ret<PageRecordsVo<AdminMenu>> page(QueryParamsAo query) {
        return Ret.ok(Rests.page(AdminMenu.class, query.page(), query));
    }

    /**
     * 新增
     * @param ao 请求参数
     * @return {@link Ret}<{@link AdminMenu}>>
     */
    @PostMapping
    public Ret<AdminMenu> create(@Validated @RequestBody MenuCreateAo ao) {
        return Ret.ok(Rests.create(AdminMenu.class, ao));
    }

    /**
     * 详情
     * @param id 编号
     * @return {@link Ret}<{@link AdminMenu}>
     */
    @GetMapping("/{id}")
    public Ret<AdminMenu> getById(@PathVariable Long id) {
        return Ret.ok(Rests.getById(AdminMenu.class, id));
    }

    /**
     * 修改
     * @param id 编号
     * @param ao 请求参数
     * @return {@link Ret}<{@link AdminMenu >
     */
    @PatchMapping("/{id}")
    public Ret<AdminMenu> patchById(@PathVariable Long id, @Validated @RequestBody MenuPatchByIdAo ao) {
        return Ret.ok(service.patchById(id, ao));
    }

    /**
     * 删除
     * @param id 编号
     * @return {@link Ret}<{@link AdminMenu}>
     */
    @DeleteMapping("/{id}")
    public Ret<AdminMenu> deleteById(@PathVariable Long id) {
        return Ret.ok(Rests.deleteById(AdminMenu.class, id));
    }

}
